<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px">
              <el-button 
              icon="el-icon-plus" 
              size="small" 
              type="primary"
              @click="showDialog=true"
                >新增角色</el-button
              >
            </el-row>
            <!-- 表格 -->
            <el-table :data="tableData" style="width: 100%">
              <!-- type:index会自动生成序号 -->
              <el-table-column label="序号" type="index" width="50" />
              <el-table-column prop="name" label="角色名称" width="180" />
              <el-table-column prop="description" label="描述" />
              <el-table-column width="300" label="操作">
                <template slot-scope="scope">
                  <el-button size="mini" type="success">分配权限</el-button>
                  <el-button size="mini">编辑</el-button>
                  <el-button
                    size="mini"
                    type="danger"
                    @click="hDel(scope.row.id)"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>

            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->
              <el-pagination
                background
                :page-size="params.pagesize"
                layout="prev, pager, next"
                :total="total"
                @current-change="fenye"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 新增弹框 -->
      <el-dialog
        title="编辑或新增弹层"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDialog"
      >
        <el-form
          ref="roleForm"
          :model="roleForm"
          :rules="rules"
          label-width="100px"
        >
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small">取消</el-button>
            <el-button size="small" type="primary" @click="hSubmit"
              >确定</el-button
            >
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getRoles, deleteRole,addRole} from "@/api/settings";
export default {
  data() {
    return {
      // 省略其他....
      showDialog: false,//控制弹层
      roleForm: { //表单的数据项
        name: "",
        description: "",
      },
      rules: {
        name: [
          { required: true, message: "角色名称不能为空", trigger: "blur" },
        ],
      },
      activeName: "first",
      tableData: [],
      params: {
        page: 1, // 查询第一页
        pagesize: 2, // 每页两条  --- 要与pagination中page-size一致
      },
      total: 10,
    };
  },
  created() {
    this.loadRoles();
    // this.doDel()
  },
  methods: {
      async doAdd(){//添加
          try{
              const res = await addRole(this.roleForm)
              console.log(res);
              this.loadRoles()
              this.$message.success('添加成功')
              this.showDialog = false
          }catch(err){
              this.$message.error('添加失败')
              console.log(err);
          }
      },
      hSubmit(){//表单验证
      this.$refs.roleForm.validate(valid =>{
          if(valid){
              this.doAdd()
          }
      })
          console.log('提交');
      },
          // 获取数据
    async loadRoles() {
      const res = await getRoles(this.params);
      // console.log('角色信息', res)
      // 保存数据
      this.tableData = res.data.rows;
    },
    fenye(curPage) {
      // 1. 更新查询条件中页码
      this.params.page = curPage;
      // console.log('查看',this.params.page);
      // 2. 再发一次请求
      this.loadRoles();
    },
    // 删除
    async doDel(id) {
      try {
        console.log("点击删除", id);
        const res = await deleteRole(id);
        console.log(res);
        this.$message({ type: "syccess", message: "删除成功" });
        // 删除后返回上一页
        if (this.tableData.length === 1) {
          this.params.page--;
          if (this.params.page <= 0) {
            this.params.page = 1;
          }
        }
        // 3. 更新页面- 重发请求
        this.loadRoles();
      } catch (err) {
        console.log(err);
        this.$message.error("删除失败");
      }
    },
    hDel(id) {
      console.log(id);
      this.$confirm("此操作将永久删除该角色, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.doDel(id);
        })
        .catch((err) => {
          console.log(err);
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style>
</style>